package dao

import (
	"context"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"rewords-server-2024/internal/model/entity"
)

type SysUploadDao struct {
	table string
}

var SysUpload = &SysUploadDao{
	table: "sys_upload",
}

// Insert 插入单条记录
func (d *SysUploadDao) Insert(ctx context.Context, data *entity.SysUpload) (id int64, err error) {
	result, err := g.DB().Model(d.table).Ctx(ctx).Data(data).Insert()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

// InsertBatch 批量插入记录
func (d *SysUploadDao) InsertBatch(ctx context.Context, data []entity.SysUpload) (err error) {
	_, err = g.DB().Model(d.table).Ctx(ctx).Data(data).Insert()
	return err
}

// GetById 根据ID获取记录
func (d *SysUploadDao) GetById(ctx context.Context, id int64) (record *entity.SysUpload, err error) {
	err = g.DB().Model(d.table).Ctx(ctx).Where("id", id).Scan(&record)
	return
}

// DeleteByIds 根据ID批量删除记录
func (d *SysUploadDao) DeleteByIds(ctx context.Context, ids []int64) (err error) {
	_, err = g.DB().Model(d.table).Ctx(ctx).WhereIn("id", ids).Delete()
	return
}

// Model 获取 Model 对象
func (d *SysUploadDao) Model(ctx context.Context) *gdb.Model {
	return g.DB().Model(d.table).Ctx(ctx)
}
